<template>
  <el-table :data="tableData" border style="width: 100%" size="mini" stripe :height="height" @selection-change="handleSelect">
    <el-table-column type="selection" width="55"></el-table-column>
    <el-table-column prop="realName" label="姓名" width="100"></el-table-column>
    <el-table-column prop="id" label="编号" width="80"></el-table-column>
    <el-table-column prop="sex" label="性别" width="70"></el-table-column>
    <el-table-column prop="phone" label="手机号" width="130"></el-table-column>
    <el-table-column prop="deportmentName" label="所在部门" width="100"></el-table-column>
    <el-table-column prop="type" label="是否参与接单" width="100">
      <template slot-scope="scope">
        <span>{{scope.row.type==1?'是':'否'}}</span>
      </template>
    </el-table-column>
    <el-table-column prop="serviceArea" label="服务区域"></el-table-column>
    <el-table-column prop="joinTime" label="入职时间" width="180"></el-table-column>
    <el-table-column prop="roleName" label="职位" width="150"></el-table-column>
    <el-table-column prop="isFullTime" label="类型" width="100"></el-table-column>
    <el-table-column prop="status" label="状态" width="80"></el-table-column>
    <el-table-column label="操作" width="250" fixed="right">
      <template slot-scope="scope">
        <el-button type="text" size="small" @click="addPersonnel('edit',scope.row)">编辑</el-button>
        <el-button type="text" size="small" @click="del(scope.row)">删除</el-button>
        <el-button type="text" size="small" @click="editPassword(scope.row)">修改密码</el-button>
        <el-button type="text" size="small" @click="prohibit(scope.row)">{{ scope.row.status=='禁用'?'启用':'禁用'}}</el-button>
        <el-button type="text" size="small" @click="setPermission(scope.row)" v-if="userInfo.isManager==1">权限设置</el-button>
      </template>
    </el-table-column>
  </el-table>
</template>
<script>
import { request } from "../../assets/request.js";
import {session} from '../../assets/session.js'
export default {
  data() {
    return {
      tableData: [],
      obj:{
        size:20,
        page:1
      },
      selList:[],
      height: window.innerHeight - (window.innerWidth <1386? 347 :283),//计算表格高度
      userInfo:"",
    };
  },
  created(){
    this.selectList(this.obj)
    this.userInfo=JSON.parse(session('get','user'))
    this.$root.$on('search',data=>{
      if(data=='no'){
        
      }else if(data=='add'){
        let obj=this.obj
        obj.page=1
      }else{
        this.obj=data
      }
      this.selectList(this.obj)
    })
  },
  methods: {
    addPersonnel(type,row) {
      //编辑员工
      this.$root.$emit("addPersonnel", type,row);
    },
    editPassword(row) {
      //修改密码
      this.$root.$emit("editPassword", row);
    },
    del(row) {
      let that=this
      request.delAlert({
          name:'删除该用户',
          that,
          fn(obj){
              let url='admin/'+row.id+'?greenToken='+request.greenToken
              request.ajaxDel({
                url,
                that,
                fn(res){
                  that.$message(obj)
                  that.selectList(that.obj);
                }
              })
          }
      })
    },
    setPermission(row){  //权限设置
        this.$root.$emit("setPermission", row)
    },
    handleSelect(val){
        this.selList = val;
        this.$root.$emit("selList",val)
    },
    prohibit(row){  //禁用启用账号
      let that=this
      let name= row.status=='禁用'?'启用该用户':'禁用该用户'
      this.$confirm("此操作将"+name+", 是否继续?", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
        }).then(() => {
          let status=row.status=='禁用'? '1' : '0'
            let url='admin/status?phone='+row.phone+'&status='+status
            request.ajaxPost({
              url,
              that,
              type:'PUT',
              fn(res) {
                that.$message({
                    type: "success",
                    message: "成功"
                });
                that.selectList(that.obj);
              }
            });
        })
        .catch(() => {
            this.$message({
                type: "info",
                message: "已取消"
            });
        });
    },
    selectList(obj) {
      let url = "admin";
      let that=this   //解决this指向问题
      request.ajaxGet({
        url,
        that,
        params:obj,
        fn(res){
          var data=res.list
          let serveRegion=that.$store.state.serveRegion
          for(let i in data){
            data[i].sex= data[i].sex=='1'? '男' : '女'
            data[i].isFullTime=data[i].isFullTime=='1'? '全职' : '兼职'
            data[i].status=data[i].status=='1'? '正常' : '禁用'
            for(let j in serveRegion){
              if(data[i].serviceArea==serveRegion[j].id){
                data[i].serviceArea=serveRegion[j].name
              }else if(data[i].serviceArea==0){
                data[i].serviceArea=''
              }
            }
            if(data[i].joinTime){
              data[i].joinTime=data[i].joinTime.split(' ')
              data[i].joinTime=data[i].joinTime[0]
            }
          }
          that.$root.$emit('selectList',res)
          that.tableData=data
        }
      })
    }
  }
};
</script>
<style lang="less" scoped>
</style>
